#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    textEdit = new QTextEdit(this);
    this->setCentralWidget(textEdit);///设置中央布局组件
    fileMenu = ui->menuBar->addMenu("文件(&F)");//添加 "文件" 菜单
    editMenu = ui->menuBar->addMenu("编辑(&E)");//添加”“编辑”菜单
    //菜单项
    //this指向父对象的指针，内存管理：当父对象被删除时，所有属于它的子对象也会被自动删除，这有助于避免内存泄漏。
    //对象树：父对象和子对象之间形成一个对象树，这有助于组织和管理对象的关系。
    //信号和槽：父对象可以更容易地管理和连接子对象的信号和槽；
    openAction = new QAction(QIcon("://imgs/opend_folder.ico"),"打开(&O)",this);
    openAction->setShortcut(QKeySequence("Ctrl+O"));//设置快捷键
    fileMenu->addAction(openAction);
    ui->mainToolBar->addAction(openAction);
    fileMenu->addSeparator();//添加分隔符
    saveAction = new QAction(QIcon(":/imgs/save.ico"),"保存(&S)",this);
    saveAction->setShortcut(QKeySequence("Ctrl+S"));
    fileMenu->addAction(saveAction);
    ui->mainToolBar->addAction(saveAction);
    saveAsAction = new QAction(QIcon(":/imgs/file_new.ico"),"另存为(&N)",this);
    saveAsAction->setShortcut(QKeySequence("Ctrl+Shift+S"));
    fileMenu->addAction(saveAsAction);
    ui->mainToolBar->addAction(saveAsAction);
    font_color = new QAction(QIcon(":/imgs/font_color.ico"),"字体颜色(&C)",this);
     editMenu->addAction(font_color);
    ui->mainToolBar->addAction(font_color);
    font_style = new QAction(QIcon(":/imgs/font.ico"),"字体样式",this);
     editMenu->addAction(font_style);
    ui->mainToolBar->addAction(font_style);




    //statusBar
    line_msg = new QLabel(this);
    line_msg ->setText("行：0,列0\t");
    ui->statusBar->addPermanentWidget(line_msg);

    label_words = new QLabel(this);
   label_words->setText("0 个字符\t\t\t");
    ui->statusBar->addPermanentWidget(label_words);

    osmsg  = new QLabel(this);
   osmsg ->setText("Windows(CRLF)\t");
    ui->statusBar->addPermanentWidget(osmsg );

    encoding  = new QLabel(this);
    encoding->setText("UTF-8\t");
    ui->statusBar->addPermanentWidget(encoding);



    //关联
    connect(openAction,SIGNAL(triggered(bool)),this,SLOT(open_action_slot()));
    connect(saveAction,SIGNAL(triggered(bool)),this,SLOT(save_action_slot()));
    connect(saveAsAction,SIGNAL(triggered(bool)),this,SLOT(save_as_action_slot()));
    connect(textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(line_msg_slot()));
    connect(font_color,SIGNAL(triggered(bool)),this,SLOT(font_color_slot()));
    connect(font_style,SIGNAL(triggered(bool)),this,SLOT(font_style_slot()));
    connect(textEdit,SIGNAL(textChanged()),this,SLOT(count_ch_slot()));
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow:: open_action_slot(){
    QString filename = QFileDialog::getOpenFileName(this,"打开文件",QDir::currentPath(),"C++ Source Files(*.cpp *.cc *.c);;C++ Head Files(*.h);;text(*.txt)");
    if(!filename.isEmpty()){
        qDebug()<< filename;
       QFile file(filename);
       if(file.open(QIODevice::ReadOnly|QIODevice::Text)){

           QTextStream in (&file);
           in.setCodec("UTF-8");
           QString content = in.readAll();
           textEdit->setPlainText(content);
           currentFile = filename;
           file.close();
       }else{
           QMessageBox:: critical(this,"Error","无法打开文件");
       }
    }else{
        qDebug() <<"没有选择文件";
    }
}
void MainWindow:: save_action_slot(){
    if(currentFile.isEmpty()){
        save_as_action_slot();
    }else{
        QFile file(currentFile);
        if(file.open(QIODevice::WriteOnly|QIODevice::Text)){
            QTextStream out(&file);
            out.setCodec("UTF-8");
            out << textEdit->toPlainText();
            file.close();
            QMessageBox ::information(this,"保存成功","文件已保存");

        }else{
            QMessageBox::critical(this,"Error","无法保存文件");
        }
    }
}
void MainWindow:: save_as_action_slot(){
    QString filename = QFileDialog::getSaveFileName(this,"另存为",QDir::currentPath());
    if(!filename.isEmpty()){
        QFile file(filename);
        if(file.open(QIODevice::WriteOnly|QIODevice::Text)){
            QTextStream out(&file);
            out.setCodec("UTF-8");
            out << textEdit->toPlainText();
            file.close();
            currentFile = filename;
            QMessageBox::information(this,"保存成功","文件已保存");
        }else {
            QMessageBox::critical(this,"ERROR","无法保存文件");
        }
    }else {
        qDebug()<<"没有选择文件";
    }
}
void MainWindow:: line_msg_slot(){
    int col = textEdit->textCursor().columnNumber();//列
    int row = textEdit->textCursor().blockNumber();//行
    qDebug()<<col<<":"<<row;
    line_msg->setText("行：" + QString::number(row) +",列" + QString::number(col)+ "\t");
}
void MainWindow::font_color_slot(){
    QColor color = QColorDialog::getColor(Qt::red,this,"选取颜色");
    if(color.isValid()){
        textEdit->setTextColor(color);
    }else{
        qDebug()<<"取消设置文字颜色";
    }
}
void MainWindow::font_style_slot(){
    bool ok;
    QFont font = QFontDialog::getFont(&ok,QFont("Arial"),this,"选择字体");
    if(ok){
        textEdit->setFont(font);
    }else{
        qDebug()<<"取消设置字体";
    }
}
void MainWindow::count_ch_slot(){
    QString text = textEdit->toPlainText();
    int characterCount = text.length();
    label_words->setText(QString::number(characterCount)+" 个字符\t\t\t");
}
